package class16;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public class Code02_DFS {

    // 图深度优先遍历
    public static void dfs(MyGraph.Node<Integer> node) {
        if (node == null) {
            return;
        }
        Stack<MyGraph.Node<Integer>> stack = new Stack<>();
        stack.push(node);
        System.out.println(node.value);
        Set<MyGraph.Node<Integer>> set = new HashSet<>();
        set.add(node);
        while (!stack.isEmpty()) {
            node = stack.pop();
            for (MyGraph.Node<Integer> next : node.nexts) {
                if (set.contains(next)) {
                    continue;
                }
                stack.push(node);
                stack.push(next);
                System.out.println(next.value);
                set.add(next);
                break;
            }
        }
    }

}
